package com.leetcode.tree.bst;

import com.leetcode.basic.TreeNode;

import java.util.*;

/**
 * @author Dennis Li
 * @date 2020/7/18 21:11
 */
public class GetMinimumDifference_530 {

    List<Integer> list = new ArrayList<>();

    public int getMinimumDifference(TreeNode root) {
        inorder(root);
        return min;
    }

    int pre = -1, min = Integer.MAX_VALUE;

    // 需要记录前一节点值的时候，注意采取中序遍历的方式
    public void inorder(TreeNode root) {
        if (root == null) return;
        inorder(root.left);
        if (pre > 0) min = Math.min(min, root.val - pre);
        pre = root.val;
        inorder(root.right);
    }

}
